using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall.Math.Geometry;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Input;
using FlatRedBall.IO;
using FlatRedBall.Instructions;
using FlatRedBall.Math.Splines;
using FlatRedBall.Utilities;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if XNA4
using Color = Microsoft.Xna.Framework.Color;
#else
using Color = Microsoft.Xna.Framework.Graphics.Color;
#endif

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
using Microsoft.Xna.Framework.Media;
#endif

// Generated Usings
using FlatRedBall.Broadcasting;
using MultiplayerTD.Entities;
using MultiplayerTD.Factories;
using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Graphics;
using FlatRedBall.Math;
using FlatRedBall.Math;
using FlatRedBall.Math;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

namespace MultiplayerTD.Screens
{
	public partial class GameField : Screen
	{
		// Generated Fields
		#if DEBUG
		static bool HasBeenLoadedWithGlobalContentManager = false;
		#endif
		Scene mMap1;
		public Scene Map1
		{
			get
			{
				if (mMap1 == null)
				{
					mMap1 = FlatRedBallServices.Load<Scene>(@"content/screens/gamefield/map1.scnx", "GameField");
				}
				return mMap1;
			}
		}
		ShapeCollection mPolygons1;
		public ShapeCollection Polygons1
		{
			get
			{
				if (mPolygons1 == null)
				{
					mPolygons1 = FlatRedBallServices.Load<ShapeCollection>(@"content/screens/gamefield/polygons1.shcx", "GameField");
				}
				return mPolygons1;
			}
		}
		SplineList mSplineList1;
		public SplineList SplineList1
		{
			get
			{
				if (mSplineList1 == null)
				{
					mSplineList1 = FlatRedBallServices.Load<SplineList>(@"content/screens/gamefield/splinelist1.splx", "GameField");
				}
				return mSplineList1;
			}
		}
		
		private Layer MobsLayer;
		private Layer TowersLayer;
		private PositionedObjectList<Mob> MobList;
		private PositionedObjectList<Tower> TowerList;
		private PositionedObjectList<Bullet> BulletList;

		public GameField()
			: base("GameField")
		{
		}

        public override void Initialize(bool addToManagers)
        {
			// Generated Initialize
			LoadStaticContent(ContentManagerName);

			MobList = new PositionedObjectList<Mob>();
			TowerList = new PositionedObjectList<Tower>();
			BulletList = new PositionedObjectList<Bullet>();
			
			
			PostInitialize();
			base.Initialize(addToManagers);
			if (addToManagers)
			{
				AddToManagers();
			}

            MobsLayer.Name = "MobsLayer";
            TowersLayer.Name = "TowersLayer";
        }
        
// Generated AddToManagers
		public override void AddToManagers ()
		{
            MobsLayer = SpriteManager.AddLayer();
            TowersLayer = SpriteManager.AddLayer();
			MobFactory.Initialize(MobList, ContentManagerName);
			TowerFactory.Initialize(TowerList, ContentManagerName);
			BulletFactory.Initialize(BulletList, ContentManagerName);
			AddToManagersBottomUp();
			CustomInitialize();
		}


		public override void Activity(bool firstTimeCalled)
		{
			// Generated Activity
			if (!IsPaused)
			{
				
				for (int i = MobList.Count - 1; i > -1; i--)
				{
					if (i < MobList.Count)
					{
						// We do the extra if-check because activity could destroy any number of entities
						MobList[i].Activity();
					}
				}
				for (int i = TowerList.Count - 1; i > -1; i--)
				{
					if (i < TowerList.Count)
					{
						// We do the extra if-check because activity could destroy any number of entities
						TowerList[i].Activity();
					}
				}
				for (int i = BulletList.Count - 1; i > -1; i--)
				{
					if (i < BulletList.Count)
					{
						// We do the extra if-check because activity could destroy any number of entities
						BulletList[i].Activity();
					}
				}
			}
			else
			{
			}
			base.Activity(firstTimeCalled);
			if (!IsActivityFinished)
			{
				CustomActivity(firstTimeCalled);
			}
			Map1.ManageAll();


				// After Custom Activity
				
            
		}

		public override void Destroy()
		{
			// Generated Destroy
			MobFactory.Destroy();
			TowerFactory.Destroy();
			BulletFactory.Destroy();
			if (MobsLayer != null)
			{
				SpriteManager.RemoveLayer(MobsLayer);
			}
			if (TowersLayer != null)
			{
				SpriteManager.RemoveLayer(TowersLayer);
			}
			for (int i = MobList.Count - 1; i > -1; i--)
			{
				MobList[i].Destroy();
			}
			for (int i = TowerList.Count - 1; i > -1; i--)
			{
				TowerList[i].Destroy();
			}
			for (int i = BulletList.Count - 1; i > -1; i--)
			{
				BulletList[i].Destroy();
			}
			Map1.RemoveFromManagers(ContentManagerName != "Global");
			Polygons1.RemoveFromManagers(ContentManagerName != "Global");
			

			base.Destroy();

			CustomDestroy();

		}

		// Generated Methods
public virtual void PostInitialize ()
{
}
public virtual void AddToManagersBottomUp ()
{
	Map1.AddToManagers(mLayer);
	Polygons1.AddToManagers(mLayer);
}
public virtual void ConvertToManuallyUpdated ()
{
	Map1.ConvertToManuallyUpdated();
	for (int i = 0; i < MobList.Count; i++)
	{
		MobList[i].ConvertToManuallyUpdated();
	}
	for (int i = 0; i < TowerList.Count; i++)
	{
		TowerList[i].ConvertToManuallyUpdated();
	}
	for (int i = 0; i < BulletList.Count; i++)
	{
		BulletList[i].ConvertToManuallyUpdated();
	}
}
public static void LoadStaticContent (string contentManagerName)
{
	#if DEBUG
	if (contentManagerName == FlatRedBallServices.GlobalContentManager)
	{
		HasBeenLoadedWithGlobalContentManager = true;
	}
	else if (HasBeenLoadedWithGlobalContentManager)
	{
		throw new Exception("This type has been loaded with a Global content manager, then loaded with a non-global.  This can lead to a lot of bugs");
	}
	#endif
	CustomLoadStaticContent(contentManagerName);
}
object GetMember (string memberName)
{
	switch(memberName)
	{
	}
	return null;
}


	}
}
